package com.intelligent.mts.client.ex;

import com.intelligent.mts.client.core.view.BomManagePanel;
import com.intelligent.mts.client.core.view.BomPNPanel;
import com.intelligent.mts.client.core.view.BomSearchPanel;
import com.intelligent.mts.client.event.DialogStatusBarEvent;
import com.intelligent.mts.client.event.StatusBarChangeEvent;
import com.intelligent.mts.client.listener.MyDialogListener;
import com.intelligent.mts.client.utils.ActionMessages;
import com.intelligent.mts.client.utils.ApplicationBeanUtils;
import com.intelligent.mts.client.utils.EventBusUtils;
import com.intelligent.mts.client.utils.ResourceBundleUtils;
import com.intelligent.mts.common.utils.ApplicationException;
import com.intelligent.mts.common.utils.ExceptionMessages;
import com.intelligent.mts.common.utils.StringUtils;
import com.intelligent.mts.core.dto.CustomerDto;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Garen.Pang on 2016/8/24.
 */
//@Component
public class MyDialog extends Dialog {

    JLabel lablerow1 = new JLabel();
    JComboBox comboxrow1 = new JComboBox();

    JLabel labelrow2 = new JLabel();
    JTextField row2 = new JTextField();

    JButton ok = new JButton("OK");
    JButton cancel = new JButton("Cancel");

    private JLabel status = new JLabel("");

    MyDialogListener listener = new MyDialogListener();


//    @Autowired
//    DialogStatusPanel status = new DialogStatusPanel();
    public MyDialog(Frame owner,boolean modal,String add,Object[] object) {
        super(owner,modal);

        EventBusUtils.register(listener);
        if ("customer".equals(add)){

            setTitle("please input Customer");
            setSize(360,180);
            setResizable(false);
            setLayout(null);

            labelrow2.setText("Customer:");
            add(labelrow2);
            add(row2);

            labelrow2.setBounds(10, 60, 65, 20);
            row2.setBounds(80, 60, 260, 25);
            add(ok);
            add(cancel);
            ok.setBounds(100, 110, 60, 25);
            cancel.setBounds(180, 110, 60, 25);
            ok.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    AddCustomerOKbtn(e);
                }
            });
            cancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    EventBusUtils.unregister(listener);
                    dispose();
                }
            });


            add(status);
            status.setBounds(20,150,320,25);

            setLocationRelativeTo(owner);

        }else if ("fgpn".equals(add)){

            setTitle("please input Finish Good");
            setSize(360,180);
            setResizable(false);
            setLayout(null);
            add(lablerow1);
            add(labelrow2);
            lablerow1.setBounds(10, 40, 65, 20);
            labelrow2.setBounds(10, 80, 65, 20);
            lablerow1.setText("Customer:");
            labelrow2.setText("FGPN:");

            for (Object o: object) {
                comboxrow1.addItem(o);
            }

            comboxrow1.setSelectedItem(ApplicationBeanUtils.getBean(BomSearchPanel.class).getCustomer().getSelectedItem());
            add(comboxrow1);
            add(row2);
//        rows.setText(Integer.toString(ta.getRows()));
//        columns.setText(Integer.toString(ta.getColumns()));
            comboxrow1.setBounds(80, 40, 260, 25);
            row2.setBounds(80, 80, 260, 25);
            add(ok);
            add(cancel);
            ok.setBounds(100, 120, 60, 25);
            cancel.setBounds(180, 120, 60, 25);
            ok.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    AddFGPNOKbtn(e);
                }
            });
            cancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    EventBusUtils.unregister(listener);
                    dispose();
                }
            });

            add(status);
            status.setBounds(20,150,320,25);
            setLocationRelativeTo(owner);
        }

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                EventBusUtils.unregister(listener);
                dispose();
            }
        });


    }

    private void AddCustomerOKbtn(ActionEvent event){

        try {
            String resultCustomer = row2.getText();

            if (StringUtils.isBlank(resultCustomer)){
                throw new ApplicationException(ExceptionMessages.EXCEPTION_ADD_CUSTOMER_CANNOT_BE_EMPTY);
            }

            if (resultCustomer.length() > 50){
                JOptionPane.showMessageDialog(null, "Input character cannot be greater than 50.",
                        "error",JOptionPane.ERROR_MESSAGE);
                return;
            }

            if(ApplicationBeanUtils.getBean(BomManagePanel.class).mainController.validateCustomer(resultCustomer)){
                CustomerDto customerDto = new CustomerDto();
                customerDto.setName(resultCustomer);
                ApplicationBeanUtils.getBean(BomManagePanel.class).mainController.createCustomer(customerDto);
                ApplicationBeanUtils.getBean(BomSearchPanel.class).initCustomer();
                ApplicationBeanUtils.getBean(BomSearchPanel.class).getCustomer().setSelectedItem(customerDto.getName());
                ApplicationBeanUtils.getBean(BomPNPanel.class).deleteAllData();
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ActionMessages.ADD_CUSTMOER_DONE)));
                EventBusUtils.unregister(listener);
                dispose();

            }else {
                throw new ApplicationException(ExceptionMessages.EXCEPTION_ADD_CUSTOMER_CANNOT_BE_REPEAT);
            }
        }catch (ApplicationException ex){
            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new DialogStatusBarEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
                EventBusUtils.post(new DialogStatusBarEvent(ResourceBundleUtils.getString(ex.getMessage()), DialogStatusBarEvent.MSG_LEVEL_ERROR));
        }
    }

    private void AddFGPNOKbtn(ActionEvent event){

        try {
            String Customer = (String) comboxrow1.getSelectedItem();
            String FGPN = row2.getText();

            if (StringUtils.isBlank(FGPN)){
                throw new ApplicationException(ExceptionMessages.EXCEPTION_ADD_FG_CANNOT_BE_EMPTY);
            }

            if (FGPN.length() > 50){
                JOptionPane.showMessageDialog(null, "Input character cannot be greater than 50.",
                        "error",JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (!ApplicationBeanUtils.getBean(BomManagePanel.class).mainController.validateFGPN(FGPN)){
                ApplicationBeanUtils.getBean(BomManagePanel.class).mainController.createFinishGood(Customer,FGPN);
                ApplicationBeanUtils.getBean(BomSearchPanel.class).intiFGPN(Customer);
                ApplicationBeanUtils.getBean(BomSearchPanel.class).getCustomer().setSelectedItem(Customer);
                ApplicationBeanUtils.getBean(BomSearchPanel.class).getFGPN().setSelectedItem(FGPN);
                ApplicationBeanUtils.getBean(BomSearchPanel.class).btnsearchclick();
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ActionMessages.ADD_FGPN_DONE)));
                EventBusUtils.unregister(listener);
                dispose();
            }else {
                throw new ApplicationException(ExceptionMessages.EXCEPTION_ADD_FG_CANNOT_BE_REPEAT);
            }
        }catch (ApplicationException ex){
            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new DialogStatusBarEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
                EventBusUtils.post(new DialogStatusBarEvent(ResourceBundleUtils.getString(ex.getMessage()), DialogStatusBarEvent.MSG_LEVEL_ERROR));
        }
    }
    public void setStatusValue(String value) {
        status.setText(value);
    }

    public void setStatusValue(String value, Color color) {
        if(color != null) {
            status.setForeground(color);
        }
        status.setText(value);
        status.setForeground(color);
    }

    public void cleanStatusValue() {
        status.setText("");
    }

}
